﻿using Microsoft.EntityFrameworkCore;
using System.ComponentModel;
using System.Linq;
// 需要在文件顶部添加
using System.Reflection;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.TenantManagement;
using Volo.Abp.TenantManagement.EntityFrameworkCore;
using WMSSystem.DYB;
using WMSSystem.WYJ;
using WMSSystem.YJ;
using WMSSystem.YSL;
using WMSSystem.Domain.Workflow;

namespace WMSSystem.EntityFrameworkCore;

[ReplaceDbContext(typeof(IIdentityDbContext))]
[ReplaceDbContext(typeof(ITenantManagementDbContext))]
[ConnectionStringName("Default")]
public class WMSSystemDbContext :
    AbpDbContext<WMSSystemDbContext>,
    IIdentityDbContext,
    ITenantManagementDbContext
{
    /* Add DbSet properties for your Aggregate Roots / Entities here. */

    #region Entities from the modules

    /* Notice: We only implemented IIdentityDbContext and ITenantManagementDbContext
     * and replaced them for this DbContext. This allows you to perform JOIN
     * queries for the entities of these modules over the repositories easily. You
     * typically don't need that for other modules. But, if you need, you can
     * implement the DbContext interface of the needed module and use ReplaceDbContext
     * attribute just like IIdentityDbContext and ITenantManagementDbContext.
     *
     * More info: Replacing a DbContext of a module ensures that the related module
     * uses this DbContext on runtime. Otherwise, it will use its own DbContext class.
     */

    //Identity
    public DbSet<IdentityUser> Users { get; set; }
    public DbSet<IdentityRole> Roles { get; set; }
    public DbSet<IdentityClaimType> ClaimTypes { get; set; }
    public DbSet<OrganizationUnit> OrganizationUnits { get; set; }
    public DbSet<IdentitySecurityLog> SecurityLogs { get; set; }
    public DbSet<IdentityLinkUser> LinkUsers { get; set; }
    public DbSet<IdentityUserDelegation> UserDelegations { get; set; }
    public DbSet<IdentitySession> Sessions { get; set; }
    // Tenant Management
    public DbSet<Tenant> Tenants { get; set; }
    public DbSet<TenantConnectionString> TenantConnectionStrings { get; set; }
    public DbSet<WarehouseManagement> WarehouseManagements { get; set; }
    public DbSet<Department> Departments { get; set; }
    public DbSet<LocationManagement> LocationManagements { get; set; }
    public DbSet<OperationLog> OperationLogs { get; set; }
    public DbSet<PositionManagement> PositionManagements { get; set; }
    public DbSet<YJ.Commoditiestable> commoditiesTables { get; set; }
    public DbSet<YJ.Transfertable> TransferTable { get; set; }
    public DbSet<YJ.Inventorytable> Inventorytable { get; set; }
    public DbSet<Inboundmanagement> Inboundmanagement { get; set; }
    public DbSet<InventoryDetails> InventoryDetails { get; set; }
    public DbSet<InventoryGoods> InventoryGoods { get; set; }
    public DbSet<QuantityList> QuantityList { get; set; }

    /// --------------------------wyj-------------------


    public DbSet<WYJ_SalesReturn> WYJ_SalesReturn { get; set; }
    public DbSet<WYJ_SalesOrder> WYJ_SalesOrder { get; set; }
    public DbSet<Statuswyj> Statuswyj { get; set; }
    public DbSet<WYJ_Buyin> WYJ_Buyin { get; set; }
    public DbSet<WYJ_GoodsforSale> WYJ_GoodsforSale { get; set; }
    public DbSet<WYJ_SalesNumber> WYJ_SalesNumber { get; set; }
    public DbSet<Outbound> Outbound { get; set; }
    public DbSet<OutbountDetails> OutbountDetails { get; set; }


    //编码生成器实体
    public DbSet<CodeRule> CodeRules { get; set; }
    //用户登录实体
    public DbSet<UserLogin> UserLogin { get; set; }

    // 工作流相关实体
    public DbSet<WorkflowInstance> WorkflowInstances { get; set; }
    public DbSet<WorkflowApproval> WorkflowApprovals { get; set; }
    
    // WorkflowCore 相关实体
    public DbSet<WorkflowInstances> WorkflowCoreInstances { get; set; }
    public DbSet<WorkflowDefinitions> WorkflowDefinitions { get; set; }
    public DbSet<WorkflowExecutionErrors> WorkflowExecutionErrors { get; set; }
    public DbSet<WorkflowExecutionPointers> WorkflowExecutionPointers { get; set; }
    public DbSet<WorkflowExtensions> WorkflowExtensions { get; set; }
    public DbSet<WorkflowScheduledCommands> WorkflowScheduledCommands { get; set; }
    public DbSet<WorkflowSubscriptions> WorkflowSubscriptions { get; set; }
    public DbSet<WorkflowEvents> WorkflowEvents { get; set; }
    public DbSet<PendingApproval> PendingApproval { get; set; }






    #endregion

    public WMSSystemDbContext(DbContextOptions<WMSSystemDbContext> options)
        : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        /* Include modules to your migration db context */

        builder.ConfigurePermissionManagement();
        builder.ConfigureSettingManagement();
        builder.ConfigureBackgroundJobs();
        builder.ConfigureAuditLogging();
        builder.ConfigureIdentity();
        builder.ConfigureOpenIddict();
        builder.ConfigureFeatureManagement();
        builder.ConfigureTenantManagement();
       

        /* Configure your own tables/entities inside here */

        //builder.Entity<YourEntity>(b =>
        //{
        //    b.ToTable(WMSSystemConsts.DbTablePrefix + "YourEntities", WMSSystemConsts.DbSchema);
        //    b.ConfigureByConvention(); //auto configure for the base class props
        //    //...
        //});


        builder.Entity<WarehouseManagement>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "WarehouseManagement", WMSSystemConsts.DbSchema).HasComment("仓库管理表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        builder.Entity<Department>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "Department", WMSSystemConsts.DbSchema).HasComment("仓管部门表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        builder.Entity<LocationManagement>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "LocationManagement", WMSSystemConsts.DbSchema).HasComment("库位管理表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        builder.Entity<OperationLog>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "OperationLog", WMSSystemConsts.DbSchema).HasComment("操作日志实体表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        builder.Entity<PositionManagement>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "PositionManagement", WMSSystemConsts.DbSchema).HasComment("仓位管理实体表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        builder.Entity<Inboundmanagement>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "Inboundmanagement", WMSSystemConsts.DbSchema).HasComment("入库管理表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        builder.Entity<InventoryDetails>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "InventoryDetails", WMSSystemConsts.DbSchema).HasComment("数量变化表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        builder.Entity<InventoryGoods>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "InventoryGoods", WMSSystemConsts.DbSchema).HasComment("货品库存表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        builder.Entity<QuantityList>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "QuantityList", WMSSystemConsts.DbSchema).HasComment("进货数量表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });



        builder.Entity<WYJ_SalesReturn>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "WYJ_SalesReturn", WMSSystemConsts.DbSchema).HasComment("销售退货表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<YJ.Commoditiestable>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "CommoditiesTable", WMSSystemConsts.DbSchema).HasComment("货品信息表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        builder.Entity<YJ.Transfertable>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "Transfertable", WMSSystemConsts.DbSchema).HasComment("商品调拨表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<YJ.Inventorytable>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "Inventorytable", WMSSystemConsts.DbSchema).HasComment("商品调拨表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<PendingApproval>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "PendingApproval", WMSSystemConsts.DbSchema).HasComment("待审批表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });




        //登录实体
        builder.Entity<UserLogin>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "UserLogin", WMSSystemConsts.DbSchema).HasComment("用户登录实体表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });


        //编码生成器实体
        builder.Entity<CodeRule>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "CodeRule", WMSSystemConsts.DbSchema).HasComment("编码生成器实体表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
       
        builder.Entity<WYJ_SalesOrder>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "WYJ_SalesOrder", WMSSystemConsts.DbSchema).HasComment("销售订单");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<Statuswyj>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "Statuswyj", WMSSystemConsts.DbSchema).HasComment("审核");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<WYJ_Buyin>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "WYJ_Buyin", WMSSystemConsts.DbSchema).HasComment("进货");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<WYJ_SalesNumber>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "WYJ_SalesNumber", WMSSystemConsts.DbSchema).HasComment("数量");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<WYJ_GoodsforSale>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "WYJ_GoodsforSale", WMSSystemConsts.DbSchema).HasComment("明细");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<Outbound>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "Outbound", WMSSystemConsts.DbSchema).HasComment("出库");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });
        builder.Entity<OutbountDetails>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "OutbountDetails", WMSSystemConsts.DbSchema).HasComment("出库数量表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        // 工作流相关实体配置
        builder.Entity<WorkflowInstance>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "WorkflowInstance", WMSSystemConsts.DbSchema).HasComment("工作流实例表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        builder.Entity<WorkflowApproval>(b =>
        {
            b.ToTable(WMSSystemConsts.DbTablePrefix + "WorkflowApproval", WMSSystemConsts.DbSchema).HasComment("工作流审批记录表");
            b.ConfigureByConvention(); //auto configure for the base class props
            //...
        });

        // WorkflowCore 相关实体配置
        builder.Entity<WorkflowInstances>(b =>
        {
            b.ToTable("WorkflowInstances").HasComment("WorkflowCore实例表");
            b.HasKey(e => e.Id);
            //...
        });

        builder.Entity<WorkflowDefinitions>(b =>
        {
            b.ToTable("WorkflowDefinitions").HasComment("WorkflowCore定义表");
            b.HasKey(e => new { e.Id, e.Version });
            //...
        });

        builder.Entity<WorkflowExecutionErrors>(b =>
        {
            b.ToTable("WorkflowExecutionErrors").HasComment("WorkflowCore执行错误表");
            b.HasKey(e => e.Id);
            //...
        });

        builder.Entity<WorkflowExecutionPointers>(b =>
        {
            b.ToTable("WorkflowExecutionPointers").HasComment("WorkflowCore执行指针表");
            b.HasKey(e => e.Id);
            //...
        });

        builder.Entity<WorkflowExtensions>(b =>
        {
            b.ToTable("WorkflowExtensions").HasComment("WorkflowCore扩展表");
            b.HasKey(e => e.Id);
            //...
        });

        builder.Entity<WorkflowScheduledCommands>(b =>
        {
            b.ToTable("WorkflowScheduledCommands").HasComment("WorkflowCore计划命令表");
            b.HasKey(e => e.Id);
            //...
        });

        builder.Entity<WorkflowSubscriptions>(b =>
        {
            b.ToTable("WorkflowSubscriptions").HasComment("WorkflowCore订阅表");
            b.HasKey(e => e.Id);
            //...
        });

        builder.Entity<WorkflowEvents>(b =>
        {
            b.ToTable("WorkflowEvents").HasComment("WorkflowCore事件表");
            b.HasKey(e => e.Id);
            //...
        });






























        // 自动将[Description]特性同步为数据库字段注释
        foreach (var entityType in builder.Model.GetEntityTypes())
        {
            var clrType = entityType.ClrType;
            if (clrType == null) continue;

            foreach (var property in clrType.GetProperties())
            {
                var descAttr = property.GetCustomAttributes(typeof(DescriptionAttribute), false)
                                       .FirstOrDefault() as DescriptionAttribute;
                if (descAttr != null)
                {
                    builder.Entity(clrType).Property(property.Name).HasComment(descAttr.Description);
                }
            }
        }
    }
}
